package com.ibeeking.gmc.global.g.rest.service.impl;

import com.ibeeking.gmc.global.g.rest.service.ITenantBankService;
import com.ibeeking.found.common.config.prop.FileOssProp;
import com.ibeeking.found.common.utils.DataUtils;
import com.ibeeking.found.common.utils.UrlUtils;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.gmc.global.service.common.constants.enums.TenantAuditStatusEnum;
import com.ibeeking.gmc.global.service.common.dos.TenantBankDO;
import com.ibeeking.gmc.global.service.common.param.TenantBankParam;
import com.ibeeking.gmc.global.service.common.vo.TenantBankVO;
import com.ibeeking.gmc.global.service.mapper.TenantBankMapper;
import com.ibeeking.nematos.core.utils.SpringBeanUtil;
import com.ibeeking.nematos.exception.BusinessException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @ClassName TenantBankServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-01-14 13:53
 **/
@Service
public class TenantBankServiceImpl extends ServiceImpl<TenantBankMapper, TenantBankDO> implements ITenantBankService {

    @Resource
    private TenantBankMapper tenantBankMapper;

    @Resource
    private FileOssProp fileOssProp;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(TenantBankParam tenantBankParam) {
        TenantBankDO tenantBankDO = BeanUtil.convertBean(tenantBankParam, TenantBankDO.class);
        tenantBankDO.setOpeningCert(UrlUtils.removeMallUrlDomainName(fileOssProp.getHost(), tenantBankDO.getOpeningCert()));
        tenantBankDO.setCreateBy(UserUtils.getUserId());
        tenantBankDO.setCreateTime(LocalDateTime.now());
        tenantBankDO.setModifyTime(LocalDateTime.now());
        tenantBankDO.setAuditStatus(TenantAuditStatusEnum.SUBMIT_NO.getValue());
        return tenantBankMapper.insert(tenantBankDO) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean modify(TenantBankParam tenantBankParam) {
        TenantBankDO tenantBankDO = BeanUtil.convertBean(tenantBankParam, TenantBankDO.class);
        tenantBankDO.setOpeningCert(UrlUtils.removeMallUrlDomainName(fileOssProp.getHost(), tenantBankDO.getOpeningCert()));
        tenantBankDO.setModifyBy(UserUtils.getUserId());
        tenantBankDO.setModifyTime(LocalDateTime.now());
        return tenantBankMapper.updateById(tenantBankDO) > 0;
    }

    @Override
    public TenantBankVO view(Long id) {
        TenantBankDO tenantBankDO = tenantBankMapper.selectById(id);
        if(tenantBankDO == null)
             return null;
        if(tenantBankDO != null)
        tenantBankDO.setOpeningCert(UrlUtils.addUrlDomainName(fileOssProp.getHost(), tenantBankDO.getOpeningCert()));
        return BeanUtil.convertBean(tenantBankDO, TenantBankVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean del(Long id) {
        return tenantBankMapper.deleteById(id) > 0;
    }

    @Override
    public Boolean delByTenantId(Long id) {
        TenantBankVO vo = this.queryByTenantId(id.intValue());
        if (vo == null) {
            throw new BusinessException("请求资源失败");
        }
        return this.del(vo.getId());
    }

    @Override
    public TenantBankVO queryByTenantId(Integer id) {
        QueryWrapper<TenantBankDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TenantBankDO::getTenantId, id);
        TenantBankVO vo = BeanUtil.convertBean(this.tenantBankMapper.selectOne(queryWrapper), TenantBankVO.class);
        if(vo != null)
           vo.setOpeningCert(UrlUtils.addUrlDomainName(fileOssProp.getHost(), vo.getOpeningCert()));
        return vo;
    }

    @Override
    public Boolean changeStatus(Integer tenantId, Integer auditStatus) {
        DataUtils.trueThrowException(tenantId == null || auditStatus == null, "审核状态修改失败!");
        return getBean().update(Wrappers.lambdaUpdate(TenantBankDO.class).eq(TenantBankDO::getTenantId, tenantId).set(TenantBankDO::getAuditStatus, auditStatus));
    }

    private static TenantBankServiceImpl getBean() {
        return SpringBeanUtil.getBean(TenantBankServiceImpl.class);
    }
}